#if !defined(SAFE_OBJECT_DELEGATE_IMPL)
#error "this file can only be included by owner.h"
#else

template <typename FunctorType, typename ParamVecType>
class AliveDelegate<FunctorType, ParamVecType, RefCountTypeTag, PARAM_COUNT> {
public:
	AliveDelegate(cosnt FunctorType &fn, const RefCountOwner &owner)
		: _fn(fn), _alive(owner.alive), _refcount(owner.refCount) {
		_refCount->fetch_add(1, std::memory_order_release);
	}

	AliveDelegate(const AliveDelegate &other)
		: _fn(other._fn), _alive(other._alive), _refCount(other.refCount) {
		_refCount->fetch_add(1, std::memory_order_release);
	}

	AliveDelegate &oprator = (const AliveDelegate &other) {
		_fn = other._fn;
		_alive = other._alive;
		_refCount = other._refCount;

		_refCount->fetch_add(1, std::memory_order_release);

		return *this;
	}

	~AliveDelegate() {
		_refCount->fetch_sub(1, std::memory_order_release);
	}

	inline bool IsSafe() {
		return _alive->load(std::memory_order_acquire) &&
			_refCount->load(std::memory_order_acquire) >= 1;
	}

	void operator()(TYPE_PARAM_LIST) const {
		if (IsSafe()) {
			try { _fn(PASS_PARAM_LIST); }
			catch (...) {}
		}
	}

private:
	FunctorType _fn;
	RefCountOwner::ref_count_value_type _refCount;
	RefCountOwner::alive_value_type _alive;
};

#endif //!defined(SAFE_OBJECT_DELEGATE_IMPL)

